#!/bin/bash
# File Name: monitor_slave_status.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2019-02-25 15:23:26
# Function: 监控slave的延迟
#########################################################################
:<<comment
变量注释:
mysql_user    : 连接mysql用户名;
mysql_passwd  : 连接mysql的密码;
result_dir    : 保存的结果目录, 将以ip+port为文件名保存该节点的监控记录
ip_port.log   : 被监控的slave的ip:port列表文件, 如192.168.1.100:3306, 表示该实例将被监控, 多个要换行
comment
mysql_user="xxxxxxxx"
mysql_passwd="xxxxxxxxxx"
result_dir="Seconds_Behind_Master"


#以下不用编辑修改
col_array=(16 6 19 22)
if [ ! -s "ip_port.log" ]
then
        echo
        echo -e "\033[31mbash ${0}: cannot access 'ip_port.log': No such file or directory\033[0m"
        echo
        exit
else
        ip_port_list=($(cat ip_port.log))
fi

#以下无需编辑修改
opt_type="${1}"

function f_format_print_info()
{
    show_str="${1}"
    if [ "$(grep -cE "^space_str|space_str$|space_strs$" <<< "${show_str}")x" == "1x" ]
    then
        show_str_len=$((${#show_str}-9))
    else
        show_str_len=${#show_str}
    fi
    max_len="${2}"
    tmp_len=$((${max_len}-${show_str_len}))
    for ((var=0;var<${tmp_len};var++))
    do
        if [ "${1}x" == "-x" ]
        then
            show_str="${show_str}-"
        else
            show_str="${show_str}space_str"
        fi
    done
}

function f_enter_str()
{
    var_pos=0
    str_type=(0 0 0)
    clo=(0 0 0)
    for var in ${@}
    do
        col_len="${col_array[${var_pos}]}"
        eval str_type[${var_pos}]="${var}"
        f_format_print_info "${str_type[${var_pos}]}" "${col_len}"
        eval clo[${var_pos}]="${show_str}"
        var_pos=$((${var_pos}+1))
    done
    show_info=""
    for ((i=0;i<${#clo[@]};i++))
    do
        if [ "${i}x" == "0x" ]
        then
            if [ "${str_type[0]}x" == "-x" ]
            then
                show_info="${show_info}\033[32m+${clo[${i}]}-+-"
            else
                show_info="\033[32m|\033[0m\033[33m${clo[${i}]}\033[0m\033[32m|\033[0m"
            fi
        elif [ "${i}x" == "$((${#clo[@]}-1))x" ]
        then
            if [ "${str_type[0]}x" == "-x" ]
            then
                show_info="${show_info}${clo[${i}]}-+\033[0m"
            else
                show_info="${show_info}\033[33m ${clo[${i}]}\033[0m\033[32m|\033[0m"
            fi
        else
            if [ "${str_type[0]}x" == "-x" ]
            then
                show_info="${show_info}${clo[${i}]}-+-"
            else
                show_info="${show_info}\033[33m ${clo[${i}]}\033[0m \033[32m|\033[0m"
            fi
        fi
    done
    echo -e "${show_info}"|sed 's/space_str/ /g'
}

function f_monitor_slave_status()
{
    [ ! -d "${result_dir}" ] && mkdir ${result_dir}
    for info in ${ip_port_list[@]}
    do
        mysql_host=$(awk -F: '{print $1}' <<< "${info}")
        mysql_port=$(awk -F: '{print $2}' <<< "${info}")
        result_file="${result_dir}/${mysql_host}_${mysql_port}.log"
        mysql_comm="mysql -u${mysql_user} -p${mysql_passwd} -h${mysql_host} -P${mysql_port} ${db_name}"
        sbm=$(${mysql_comm} -e "show slave status\G" 2>/dev/null|grep Seconds_Behind_Master|awk -F': ' '{print $2}')
        [ "${sbm}x" == "x" ] && sbm="NULLspace_str" || sbm="${sbm}space_strs"
        if [ "${opt_type}x" == "allx" ]
        then
            [ -f "${result_file}" ] && cat ${result_file}
        fi
        f_enter_str "space_str${mysql_host}" "${mysql_port}" "$(date "+%F_%H:%M:%S")" "${sbm}"|tee -a ${result_file}
        f_enter_str "-" "-" "-" "-"
    done
}

while :
do
    f_enter_str "-" "-" "-" "-"
    f_enter_str "space_strhost_ip" "port" "current_time" "Seconds_Behind_Masterspace_str"
    f_enter_str "-" "-" "-" "-"
    f_monitor_slave_status
    sleep 10
done
